package message

import (
	"fmt"
	"os"
	"strings"
	"time"
)

const (
	NotifyProductAdded  = "ProductAdded"
	NotifyProductDelete = "ProductDelete"
	NotifyAddDevice     = "AddDevice"
	NotifyDeleteDevice  = "DeleteDevice"
	NotifyAction        = "Action"
	NotifySerialNet     = "SerialNet"
	NotifyCheckStatus   = "CheckStatus"
	NotifySimulatorSend = "SimulatorSend"
	NotifyUpgrade       = "Upgrade"
)

type Time time.Time

var loc *time.Location

func init() {
	loc, _ = time.LoadLocation("Asia/Shanghai")
}

func (t Time) MarshalJSON() ([]byte, error) {
	var stamp = fmt.Sprintf(`"%s"`, time.Time(t).Format("2006-01-02 15:04:05"))
	return []byte(stamp), nil
}

func (t *Time) UnmarshalJSON(b []byte) error {
	tm, err := time.ParseInLocation("2006-01-02 15:04:05", strings.TrimSpace(string(b[1:len(b)-1])), loc)
	if err != nil {
		return err
	}
	*t = Time(tm)
	return nil
}

type Downstream struct {
	DalID       string      `json:"dalId"`
	NotifyType  string      `json:"notifyType"`
	ProductKey  string      `json:"productKey"`
	Sn          string      `json:"sn"`
	VersionName string      `json:"versionName"`
	MessageID   string      `json:"messageId"`
	Time        Time        `json:"time"`
	Data        interface{} `json:"data"`
}

type DsDeviceActionData struct {
	ActionCode string
	Params     map[string]interface{}
}

type DsDeviceAddData struct {
	Name   string
	Remark string
}

type DsSerialNetData struct {
	Data    string
	DataLen int32
}

type DsSimulatorSendData struct {
	Content string
	Binary  bool
}

type DsUpgradeData struct {
	UpgradeID string
	Firmsoft  *FirmsoftInfo
	File      *os.File
	SnList    []string
}

type FirmsoftInfo struct {
	FirmsoftID string
	Name       string
	Version    string
	SignAlgo   string
	Sign       string
}

type DsUpgradeInData struct {
	UpgradeID string
	Firmsoft  *FirmsoftInfo
	SnList    []interface{}
}
